﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DOMercury.Managers;

namespace DOMercury.Forms
{
    public partial class DisplayOptions : Form
    {
        private string nskin;
        public DisplayOptions()
        {
            InitializeComponent();
            chkFade.Checked = ConfigManager.Fade;
            chkDR.Checked = ConfigManager.DescriptiveResults;
            NUDDelay.Value = Convert.ToDecimal(ConfigManager.STI);
            lblCS.Text = "Current Skin: " + Path.GetFileNameWithoutExtension(ConfigManager.CurrentSkin);
            txtOpacity.Text = ConfigManager.Opacity.ToString();
            GetSkins();
        }

        private void GetSkins()
        {
            foreach (string s in Directory.GetDirectories("Skins/"))
            {
                foreach (string f in Directory.GetFiles(s))
                {
                    if (Path.GetExtension(f) == ".skin")
                    {
                        lbSkins.Items.Add(new Skin(Path.GetFileNameWithoutExtension(f), f));
                    }
                }
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool restart = false;
            ConfigManager.DescriptiveResults = chkDR.Checked;
            ConfigManager.Fade = chkFade.Checked;
            ConfigManager.STI = Convert.ToInt32(NUDDelay.Value);
            double newVal;
            Double.TryParse(txtOpacity.Text.Trim(),out newVal);
            if( newVal != null)
            {
                if (newVal != ConfigManager.Opacity)
                {
                    ConfigManager.Opacity = newVal;
                    if (ConfigManager.Opacity > 1)
                    {
                        ConfigManager.Opacity = 1;
                    }
                    else if (ConfigManager.Opacity < 0)
                    {
                        ConfigManager.Opacity = 0;
                    }
                    restart = true;
                }
            }
            if (nskin != ConfigManager.CurrentSkin)
            {
                if (nskin != null)
                {
                    ConfigManager.CurrentSkin = nskin;                   
                    SkinManager.ReadSkin(nskin);
                    restart = true;
               }
                
            }
            ConfigManager.SaveCurrentConfiguration();
            if (restart)
            {
                DOMercury.Engine.Interface.EngineCore.RestartInterface();
            }
            this.Hide();
        }

        private void lbSkins_DoubleClick(object sender, EventArgs e)
        {
            nskin = ((Skin)(lbSkins.Items[lbSkins.SelectedIndex])).path;
            lblCS.Text = "Current Skin: " + ((Skin)(lbSkins.Items[lbSkins.SelectedIndex])).name;

        }
    }

    public class Skin
    {
        public string name;
        public string path;
        public Skin(string Name, string Path)
        {
            name = Name;
            path = Path;
        }

        public override string ToString()
        {
            return name;
        }
    }

}
